package com.wjm.dynamicskin.skin;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.wjm.dynamicskin.skin.util.SkinThemeUtils;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

//获得界面所有控件的工厂SkinLayoutFactory继承自LayoutInflater.Factory并在回调方法中得到个界面中所有的view对象23333333333333333333333333
public class SkinLayoutFactory implements LayoutInflater.Factory2, Observer {

    private Activity activity;

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {//

        //获得界面所有控件的工厂SkinLayoutFactory继承自LayoutInflater.Factory并在回调方法中得到个界面中所有的view对象23333333333333333333333333
        //拿到界面上的view对象view
        //反射 classloader
        View view = createViewFromTag(name, context, attrs);
        //如果从mClassPrefixList数组中那几个包名拼接创建不出来这个view对象，那么这个view是一个自定义的view,那么就不用拼接包名了，直接创建view调用createView方法
        // 自定义View则直接创建
        if (null == view) {
            view = createView(name, context, attrs);
        }

        //以上是创建出了界面中所有的view，但不是所有view都是要换肤的，所以要筛选符合属性的View4444444444444444444444444444444444444444444444
        //筛选符合属性的View
        skinAttribute.load(view, attrs);

        //我们把皮肤包做成一个apk，那么怎么从一个外部的apk中去加载资源呢？
        return view;
    }

    @Override
    public View onCreateView(String s, Context context, AttributeSet attributeSet) {
        return null;
    }

    @Override
    public void update(Observable observable, Object o) {
        /**
         * StatusBar&NavigationBar换肤
         */
        SkinThemeUtils.updateStatusBar(activity);
        //字体要换也要更新
        Typeface typeface = SkinThemeUtils.getSkinTypeface(activity);
        // 更换皮肤
        skinAttribute.applySkin(typeface);
    }

    // 属性处理类
    SkinAttribute skinAttribute;

    public SkinLayoutFactory(Activity activity,Typeface typeface) {
        this.activity = activity;
        skinAttribute = new SkinAttribute(typeface);
    }


    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.view.",
            "android.webkit."
    };

    private View createViewFromTag(String name, Context context, AttributeSet attrs) {
        //包含了 . 自定义控件
        if (-1 != name.indexOf(".")) {//如果是自定义控件就不管了
            return null;
        }
        View view = null;
        for (int i = 0; i < mClassPrefixList.length; i++) {//把view的全类名拼接出来
            view = createView(mClassPrefixList[i] + name, context, attrs);
            if (null != view) {
                break;
            }
        }
        return view;
    }

    private static final Class<?>[] mConstructorSignature = new Class[]{
            Context.class, AttributeSet.class};
    private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
            new HashMap<String, Constructor<? extends View>>();

    private View createView(String name, Context context, AttributeSet attrs) {
        Constructor<? extends View> constructor = sConstructorMap.get(name);
        if (null == constructor) {
            try {
                Class<? extends View> aClass = context.getClassLoader().loadClass(name).asSubclass
                        (View.class);
                constructor = aClass.getConstructor(mConstructorSignature);
                sConstructorMap.put(name, constructor);
            } catch (Exception e) {
            }
        }
        if (null != constructor) {
            try {
                return constructor.newInstance(context, attrs);
            } catch (Exception e) {
            }
        }
        return null;
    }
}
